<?php
require_once("mantis_config.php");

$mantis = new PHPXMLRPCClient();

$mantis->createClient($web_path_to_mantis_xmlrpc . "mantisserver.php", $domain);

$mantis->setXMLRPCDebug(0);

global $project_id, $task_id, $mantis_url;

$AppUI->savePlace();
/*
$titleBlock = new CTitleBlock( 'Mantis', 'mantis_logo_button.gif', $m, "$m.$a" );
$titleBlock->show();
*/

$project_id = "";
if (!empty($_REQUEST['project_id'])) {
	$project_id = $_REQUEST['project_id'];
}

$userid = $AppUI->user_id;
$query1= "SELECT user_username FROM users WHERE user_id = '$userid' " ;
$result1 = mysql_query( $query1 )or die(mysql_error());
while ($row1 = mysql_fetch_array($result1, MYSQL_NUM)) {
	$username = $row1[0];
}
$query2= "SELECT contact_email FROM contacts WHERE contact_id = '$userid' " ;
$result2 = mysql_query( $query2 )or die(mysql_error());
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
	$email= $row2[0];
}

function show_issue($summary,$bugid,$username,$email)
{
    $mantispath = dPgetConfig('mantis_path') ;
    $parm = $username ;
    $parm .="||";
    $parm .= $bugid;
    $parm .="||";
    $parm .= $email;
    $parm .="||";
    $parm = base64_encode($parm) ;
    $link  = "<a href=" ;
    $link .= $mantispath ;
    $link .= "/index_dp.php?parm=";
    $link .= $parm ;
    $link .= " target=_blank";
    $link .= ">";
    $link .= $summary ;
    $link .= "</a>";
    return $link;
}


$query = "SELECT project_name, project_id FROM projects" ;
$result = mysql_query($query)or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
	$all_dp_projects[$row["project_id"]] = trim(strtolower($row["project_name"]));
	
	if($row["project_id"] == $project_id){
		$project_name = $row["project_name"];
	}
}


// if there is a project name, we want to get the bugs for that project
if($project_name != ""){
	
	$mantis->resetRequest();
	$mantis->setFunction("MantisRPC");
	$mantis->addArg(array($mantis_user, $mantis_pass));
	$mantis->addArg("getMantisBugByProjectName");
	$mantis->addArg($project_name);
	
	$bugs = $mantis->call();
	
	if(ERROR::isError($bugs)){
		die($bugs->getErrstr());
	}
	
}
// if there is no project name, get all bugs for all projects
else {
	$mantis->resetRequest();
	$mantis->setFunction("MantisRPC");
	$mantis->addArg(array($mantis_user, $mantis_pass));
	$mantis->addArg("getMantisBugByProjectID");
	$mantis->addArg("");
	
	$bugs = $mantis->call();
	
	if(ERROR::isError($bugs)){
		die($bugs->getErrstr());
	}
}

if($bugs == 0){
?>
<br /><br />This project was not found in Mantis.<br /><br />
<input class="button" type="button" value="<?php echo $AppUI->_('Create Project In Mantis'); ?>" onClick="window.location='?m=mantis&a=addproject&project_id=<?php echo $project_id?>'">
<br />
<br />
<?php
}
else {
	
	if(is_array($bugs)){
		$bugs_dp = array();

		foreach($bugs as $b){
			if(in_array(trim(strtolower($b["project_name"])), $all_dp_projects)){
				$b["dp_project_id"] = array_search(trim(strtolower($b["project_name"])), $all_dp_projects);
				$bugs_dp[] = $b;
			}
		}
		
		$bugs = $bugs_dp;
	}
	
	if(is_array($bugs) && count($bugs) > 0){
?>
<br />
<form method="POST" action="?m=mantis&a=editbugs">
<input type="hidden" name="project_id" value="<?php echo $project_id?>">
<table width="100%" border="0" cellpadding="2" cellspacing="1" class="tbl">
<tr>
	<th width="50"><?php echo $AppUI->_('ID');?></th>
	<th width="120"><?php echo $AppUI->_('Submit Date');?></th>
	<th width="150"><?php echo $AppUI->_('Project');?></th>
	<th width="75"><?php echo $AppUI->_('User');?></th>
	<th width="100"><?php echo $AppUI->_('Status');?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_('Summary');?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_('Description');?></th>
	<th width="50"><?php echo $AppUI->_('Selection'); ?></th>
</tr>
<?php
		foreach($bugs as $bug){
		    
			$bugid = str_pad($bug["id"], 7, "0", STR_PAD_LEFT);
			$bugprojname = $bug["project_name"];
			$bugsummary = $bug["summary"];
			$bugdesc = $bug["description"];
			$buguser = $bug["reporter_name"];
			$bugdate = date("n/j/y g:ia", $bug["date_submitted"]);
			$bugstat = $bug["status"];
			$dbprojid = $bug["dp_project_id"];
		    
		    
		    if(strlen($bugsummary) > 40){
			    $bugsummary = substr($bugsummary, 0, 40) . "...";
		    }
		    
		    
		    if(strlen($bugdesc) > 80){
			    $bugdesc = substr($bugdesc, 0, 80) . "...";
		    }
	            	
	    	switch ($bugstat) {
	    	case 10:
	    		$status = 'New';
	    		$status_color = '#ffa0a0'; # red
	    		break;
	    	case 20:
	    		$status = 'Feedback';
	    		$status_color = '#ff50a8'; # purple
	    		break;
	    	case 30:
	    		$status = 'Acknowledged';
	    		$status_color = '#ffd850'; # orange
	    		break;
	    	case 40:
	    		$status = 'Confirmed';
	    		$status_color =  '#ffffb0'; # yellow
	    		break;
	    	case 50:
	    		$status = 'Assigned';
	    		$status_color = '#c8c8ff'; # blue
	    		break;
	    	case 80:
	    		$status = 'Resolved';
	    		$status_color = '#cceedd'; # buish-green
	    		break;
	    	case 90:
	    		$status = 'Closed';
	    		$status_color = '#e8e8e8'; # light gray
	    		break;
	    	}
	
?>

    <tr>
    	<td align="right"><?php echo show_issue($bugid,$bugid,$username,$email); ?></td>
    	<td align="center"><?php echo $bugdate ?></td>
    	<td align="center"><a href="?m=projects&a=view&project_id=<?php echo $dbprojid; ?>"><?php echo $bugprojname ?></a></td>
    	<td align="center"><?php echo $buguser ?></td>
    	<td align="center" style="background-color:<?php echo $status_color; ?>"><strong><b>
    	<?php echo $status ?>
    	</b></strong></td>
    	<td><?php echo show_issue($bugsummary,$bugid,$username,$email); ?></td>
    	<td><?php echo $bugdesc ?></td>
    	<td align="center"><input type="checkbox" name="bugs[]" value="<?php echo $bugid; ?>"></td>
    </tr>
<?php
		}
// connect again to DP  database
db_connect( $dPconfig['dbhost'], $dPconfig['dbname'],
	$dPconfig['dbuser'], $dPconfig['dbpass'], $dPconfig['dbpersist'] );

?>
</table>
<p align="right">
<input class="button" type="button" name="btnFuseAction" value="<?php echo $AppUI->_('Edit Selections'); ?>" onClick="submit()">
<select size="1" name="bug_action" id="bug_action" class="text">
<?php
if($project_id != ""){
?>
	<option value="task">Convert Selected to Tasks</option>
	<option value="">------------------------</option>
<?php
}
?>
	<option value="10">Status => New</option>
	<option value="20">Status => Feedback</option>
	<option value="30">Status => Acknowledged</option>
	<option value="40">Status => Confirmed</option>
	<option value="50">Status => Assigned</option>
	<option value="80">Status => Resolved</option>
	<option value="90">Status => Closed</option>
</select>
</p>
</form>
<?php
	}
	else {
?>
<br />
<br />
No bugs were found for this project.
<br />
<br />
<?php
	}	
}
?>